// pages/recent-watch/recent-watch.js
var isEnd = false ;
//请求列表数据的方法
var loadList = function(that){
  if (isEnd){ //如果加载的数据少于20条 不会再有下面的请求
    return false;
  }
// 每一次下拉加载请求的参数都不一样
  util.ajax({
    url: '',
    data: {
      page_num:that.data.page_no,
      size:20
    },
    header: {
      'content-type': 'application/json' // 默认值
    },
    success: function (res) {
      if(res && res.data && res.data.list){
        var eleList = res.data.list;
        //如果是第一次加载
        if (that.data.isFirstLoad) {
          if (eleList.length<20){  //第一次加载的数据少于20条 
            isEnd:true
          }
          that.setData({
            eleList: eleList,
            isFirstLoad: false
          })
        } else {
          if (eleList.length<20){  //非第一次加载数据少于20条
            isEnd: true
          }
          var eleListMore = that.data.eleList.concat(eleList)
            that.data.page_no++ ;  //请求的页码也会相应的叠加
            that.setData({
              eleList: eleListMore
            })

        }
      }
     
    }
  })
};
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page_no:1,     //请求页码初始化
    scrollHeight: '',
    toView: 'red',
    scrollTop: 0,
    isFirstLoad: true,  //判断是否为第一次加载
    eleList:[],
    eleList1: [
      {        src:
'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
        name: '天文直播间',
        time: '2018 - 03 - 22 22: 30',
        recentTime: '2018 - 03 - 22 22: 30'
      },
      {
        src: 'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
        name: '天文直播间',
        time: '2018 - 03 - 22 22: 30',
        recentTime: '2018 - 03 - 22 22: 30'
      },
      {
        src: 'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
        name: '天文直播间',
        time: '2018 - 03 - 22 22: 30',
        recentTime: '2018 - 03 - 22 22: 30'
      },
      {
        src: 'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
        name: '天文直播间',
        time: '2018 - 03 - 22 22: 30',
        recentTime: '2018 - 03 - 22 22: 30'
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var scrollHeight = wx.getSystemInfoSync().windowHeight - 15;
    that.setData({
      scrollHeight: scrollHeight
    })
    //调用加载列表的方法
    // loadList(that);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },
  //下拉刷新
  upperLower: function () {
    var that = this;
    console.log('滑到顶部')
    that.data.isFirstLoad = true ; //下拉刷新请求都是第一次请求
    that.data.eleList = '' ;
    // loadList(that);
  },
  //上拉加载
  lowerLoad: function () {
    var that = this ;
    console.log('滑到底部')
   // loadList(that);
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})